package io.jee.caisan.weixin.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import io.jee.alaska.util.Result;
import io.jee.caisan.weixin.domain.SysActivity;
import io.jee.caisan.weixin.repository.SysActivityRepository;
import io.jee.caisan.weixin.service.SysActivityService;

@Service
@Transactional
public class SysActivityServiceImpl implements SysActivityService{
	
	@Resource
	private SysActivityRepository sysActivityRepository;

	@Override
	public Page<SysActivity> findPage(String type, Pageable pageable) {
		return sysActivityRepository.findAll(new Specification<SysActivity>() {
			private static final long serialVersionUID = 1L;

			@Override
			public Predicate toPredicate(Root<SysActivity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Set<Predicate> predicates = new HashSet<>();
                if(StringUtils.hasText(type)){
                    predicates.add(cb.equal(root.get("type"), type));
                }
                Predicate[] restrictions = new Predicate[predicates.size()];
                return query.where(predicates.toArray(restrictions)).getRestriction();
			}
		}, pageable);
	}

	@Override
	public Result<SysActivity> add(SysActivity sysActivity) {
		if(!StringUtils.hasText(sysActivity.getCode())) {
			return Result.error("活动代码不可为空");
		}
		SysActivity entity = sysActivityRepository.select().where("code", sysActivity.getCode()).end().unique();
		if(entity != null) {
			return Result.error("活动代码不可重复");
		}
		sysActivity.setEnable(true);
		sysActivityRepository.save(sysActivity);
		return Result.success(sysActivity);
	}

	@Override
	public Result<SysActivity> update(SysActivity sysActivity) {
		SysActivity codeEntity = sysActivityRepository.select().where("code", sysActivity.getCode()).end().unique();
		if(codeEntity != null && codeEntity.getId() != sysActivity.getId()) {
			return Result.error("活动代码不可重复");
		}
		SysActivity entity = sysActivityRepository.findById(sysActivity.getId()).orElse(null);
		BeanUtils.copyProperties(sysActivity, entity, new String[] {"id", "enable"});
		sysActivityRepository.save(entity);
		return Result.success(entity);
	}

	@Override
	public Result<Void> delete(Integer[] ids) {
		sysActivityRepository.deleteBatch(Arrays.asList(ids));
		return Result.success();
	}

	@Override
	public Result<Integer> updateEnable(Integer id, boolean enable) {
		sysActivityRepository.update().set("enable", enable).where("id", id).end();
		return Result.success(id);
	}

	@Override
	public SysActivity findOneByTypeAndCode(Integer type, String code) {
		List<SysActivity> list = sysActivityRepository.select().where("type", type).and("code", code).and("enable", true).end().list();
		if(!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}

}
